package gov.pbc.nn.kjgl.jfcr.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gov.pbc.nn.core.dao.AbstractHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.kjgl.entity.ComputerRoom;
import gov.pbc.nn.kjgl.entity.ComputerRoomApplyEntry;
import gov.pbc.nn.kjgl.entity.ComputerRoomArea;
import gov.pbc.nn.kjgl.entity.ComputerRoomEntryType;
import gov.pbc.nn.kjgl.entity.DeviceRecord;
import gov.pbc.nn.kjgl.entity.SignOut;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

/**
 * 申请表的后太类，因为流程涉及的少，所以就写一个后台dao，和pr
 * 
 * @author jack.zhao
 * 
 */
@Repository
public class ApplyEntryDao extends AbstractHibernateDao {

	@Autowired
	@Qualifier(PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao pbcnn;

	public List<ComputerRoomArea> getBycId(String crid) {
		String hql = "from ComputerRoomArea where crId=:crId and invalid =false";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("crId", crid);
		return this.query(hql, map);
	}

	public ComputerRoomApplyEntry loadApplyById(String bussinessId) {
		String hql = "from ComputerRoomApplyEntry where id=:bussinessId and invalid =false";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("bussinessId", bussinessId);
		List<ComputerRoomApplyEntry> lists = this.query(hql, map);
		if (lists.size() == 0) {
			return new ComputerRoomApplyEntry();
		} else {
			return lists.get(0);
		}
	}

	public ComputerRoomApplyEntry getEntity() {
		String hql = "from ComputerRoomApplyEntry where  invalid =false";
		List<ComputerRoomApplyEntry> lists = this.query(hql);
		if (lists.size() == 0) {
			return new ComputerRoomApplyEntry();
		} else {
			return lists.get(0);
		}
	}

	public List<ComputerRoom> getRoom() {
		String hql = "from ComputerRoom where invalid =false";
		return this.query(hql);
	}

	public List<ComputerRoomEntryType> getType() {
		String hql = "from ComputerRoomEntryType where invalid =false";
		return this.query(hql);
	}

	public List<ComputerRoomArea> getArea(String crId) {
		if (StringUtils.isEmpty(crId)) {
			// String hql = "from ComputerRoomArea where invalid =false";
			// return this.query(hql);
			return null;
		} else {
			String hql = "from ComputerRoomArea where invalid =false and crId=:crId";
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("crId", crId);
			return this.query(hql, map);
		}
	}

	// public void getArea(Page<ComputerRoomArea> page,String crId){
	// DetachedCriteria dc =DetachedCriteria.forClass(ComputerRoomArea.class);
	// dc.add(Restrictions.or(Restrictions.eq("invalid", false),
	// Restrictions.isNull("invalid")));
	// dc.add(Restrictions.eq("crId", crId));
	// }

	public void saveAll(ComputerRoomApplyEntry list) {
		// EntityState state = EntityUtils.getState(list);
		// if (state.equals(EntityState.NEW)) {
		this.save(list);
		// } else if (state.equals(EntityState.DELETED)) {
		// this.delete(list);
		// } else if (state.equals(EntityState.MODIFIED)) {
		// this.update(list);
		// }
	}

	public void saveDevice(DeviceRecord list) {
		EntityState state = EntityUtils.getState(list);
		if (state.equals(EntityState.NEW)) {
			this.save(list);
		} else if (state.equals(EntityState.DELETED)) {
			this.delete(list);
		} else if (state.equals(EntityState.MODIFIED)) {
			this.update(list);
		}
	}

	public void saveSigOut(SignOut list) {
		EntityState state = EntityUtils.getState(list);
		if (state.equals(EntityState.NEW)) {
			this.save(list);
		} else if (state.equals(EntityState.DELETED)) {
			this.delete(list);
		} else if (state.equals(EntityState.MODIFIED)) {
			this.update(list);
		}
	}

	// 得到分页方法
	public void getPage(Page<ComputerRoomApplyEntry> page, Criteria criteria) {
		// DetachedCriteria query =
		// DetachedCriteria.forClass(ComputerRoomApplyEntry.class);
		DetachedCriteria query = this.buildDetachedCriteria(criteria, ComputerRoomApplyEntry.class);
		query.add(Restrictions.or(Restrictions.eq("invalid", false), Restrictions.isNull("invalid")));
//		query.add(Restrictions.eq("state", "Over"));
		this.pagingQuery(page, query);
	}

	public List<ComputerRoomApplyEntry> getAllList() {
		String hql = "from ComputerRoomApplyEntry where invalid =false  ORDER BY applyDate ";
		return this.query(hql);
	}

	public ComputerRoomApplyEntry getAll(String id) {
		String hql = "from ComputerRoomApplyEntry where invalid =false and id=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<ComputerRoomApplyEntry> lists = this.query(hql, map);
		if (lists.size() <= 0) {
			return null;
		} else {
			return lists.get(0);
		}
	}

	public List<DeviceRecord> getDevices(String id) {
		String hql = "from DeviceRecord where invalid =false and craeId=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return this.query(hql, map);
	}

	public List<SignOut> getsignOuts(String id) {
		String hql = "from SignOut where invalid =false and applyId=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return this.query(hql, map);
	}

	public void saveSignOutList(ComputerRoomApplyEntry entity) {
		List<SignOut> lists = EntityUtils.getValue(entity, "signOut");
		if (lists != null && lists.size() > 0) {
			for (SignOut sigOut : lists) {
				String sign = EntityUtils.getValue(sigOut, "sign");
				sigOut.setApplyId(entity.getId());
				if (StringUtils.isNotEmpty(sign)) {
					sigOut.setSignImg(sign.getBytes());
				}
//				this.save(sigOut);
				this.saveSigOut(sigOut);
			}
		}
	}

	public List<SignOut> getSignOutById(String id) {
		String hql = "from SignOut where invalid =false and applyId=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		return this.query(hql, map);
	}

	// changeState

	//30是流程结束
	public void changeState(String id) {
		ComputerRoomApplyEntry computerroomapplyentry = this.getComputerRoomApplyEntryById(id);
		computerroomapplyentry.setState(30);
		this.update(computerroomapplyentry);
	}

	public ComputerRoomApplyEntry getComputerRoomApplyEntryById(String id) {
		String hql = "from ComputerRoomApplyEntry where invalid=false and id=:id";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<ComputerRoomApplyEntry> ls = this.query(hql, map);
		if (ls.size() > 0) {
			return ls.get(0);
		} else {
			return null;
		}
	}

	public String getCnameByMamberId(String mamberId) {
		String hql = "select cname from User where username=:mamberId";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mamberId", mamberId);
		List<String> ls = pbcnn.query(hql, map);
		if (ls != null && !ls.isEmpty()) {
			return ls.get(0);
		}
		return null;
	}
}
